import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import { createHtmlPlugin } from "vite-plugin-html";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { vitePluginForArco } from '@arco-plugins/vite-vue'
import { ArcoResolver } from "unplugin-vue-components/resolvers";

// https://vitejs.dev/config/

const resolve = (dir) => path.resolve(__dirname, dir);
const getEnvValue = (mode, key) => loadEnv(mode, process.cwd())[key];

export default defineConfig(({ mode }) => {
  return {
    resolve: {
      alias: {
        "@": resolve("src"), //作为 entries 的选项
      },
      extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
    },
    plugins: [
      vue(),
      vitePluginForArco({
        style: 'css'
      }),
      AutoImport({
        resolvers: [ArcoResolver()],
      }),
      Components({
        resolvers: [
          ArcoResolver({
            sideEffect: true,
          }),
        ],
      }),
      createHtmlPlugin({
        minify: false,
        inject: {
          data: {
            title: getEnvValue("VITE_APP_TITLE"),
          },
        },
      }),
    ],
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            'arcoblue-6': '#f85959',
          },
          javascriptEnabled: true,
        },
        scss: {
          additionalData: ` @import "@/styles/index.scss";   `,
        },
      },
    },
    server: {
      proxy: {
        "/api": {
          target: getEnvValue("VITE_APP_BASE_URL"),
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
  };
});
